#endcoding:utf-8
class EvaluatesController < ApplicationController
  def new
    @demand = Demand.find(params[:demand_id])
    if @demand.evaluated
      flash[:danger] = "这个订单已经评价过了，每个订单只能评价一次"
      redirect_to root_url
    end
    @user = Offer.find_by_sql('select * from users where id= (select user_id from offers where id=(select offer_id from deals where demand_id='+params[:demand_id].to_s+'))')[0]
    @evaluater = Evaluate.new
  end

  def create
    ActiveRecord::Base.transaction do
      @evaluater = Evaluate.new(evaluate_params)
      if @evaluater.save
        content = message_link "厂家对你做出了评价","/usercard/"+params[:evaluate][:evaluated_id].to_s
        send_message params[:evaluate][:evaluated_id],content,params[:evaluate][:demand_id]
        redirect_to "/usercard/"+params[:evaluate][:evaluated_id].to_s
      end
    end
  end

  private
  def evaluate_params
    params.require(:evaluate).permit(:evaluater_id,:evaluated_id,:comment)
  end
end
